package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.uis.activities.GroupNoticeEditActivity;
import com.sam.im.heiqun.uis.beans.NoticeBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/8/17.
 */

public class NoticeGroupAdapter extends RecyclerView.Adapter<NoticeGroupAdapter.NoticeViewHolder> {

    private Context context ;
    private List<NoticeBean> mList ;
    private LayoutInflater inflater;

    public interface OnClickListener{
        void OnClick(NoticeBean noticeBean);
    }

    private OnClickListener onClickListener;
    public void setOnClickListener(OnClickListener mOnClickListener){
        this.onClickListener = mOnClickListener;
    }

    public interface OnLongClickListener{
        void OnLongClick(NoticeBean noticeBean);
    }

    private OnLongClickListener onLongClickListener;
    public void setOnLongClickListener(OnLongClickListener mOnLongClickListener){
        this.onLongClickListener = mOnLongClickListener;
    }

    public NoticeGroupAdapter(Context context, List<NoticeBean> mList){
        this.context = context ;
        if (null==mList)mList = new ArrayList<>();
        this.mList = mList ;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public NoticeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_group_notice, parent, false);
        NoticeViewHolder viewHolder = new NoticeViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(NoticeViewHolder holder, int position) {
        if (position < mList.size()){
            final NoticeBean noticeBean = mList.get(position);
            holder.txt_title.setText(noticeBean.getTitle());
            holder.txt_content.setText(noticeBean.getContent());
            holder.txt_content.setText(noticeBean.getContent());
            holder.txt_time.setText(getTime(Long.parseLong(noticeBean.getCreateTime())));
//
            holder.mview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null!=onClickListener)onClickListener.OnClick(noticeBean);
                }
            });

            holder.mview.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (null!=onLongClickListener)onLongClickListener.OnLongClick(noticeBean);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null==mList?0:mList.size();
    }

    private String getTime(long time){
        SimpleDateFormat format=new SimpleDateFormat("MM-dd");
        Date d1=new Date(time);
        return format.format(d1);
    }

    class NoticeViewHolder extends RecyclerView.ViewHolder {

        private TextView txt_title;
        private TextView txt_time;
        private TextView txt_content;

        private View mview ;

        public NoticeViewHolder(View view) {
            super(view);
            mview = view ;
            txt_title = (TextView) view.findViewById(R.id.txt_title);
            txt_time = (TextView) view.findViewById(R.id.txt_time);
            txt_content = (TextView) view.findViewById(R.id.txt_content);
        }
    }

}
